package cn.dakaqi.entities;

import cn.dakaqi.utils.qrcode.ImageType;
import cn.dakaqi.utils.qrcode.QRCode;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.io.ByteArrayOutputStream;
import java.util.Date;

/**
 * Created by chunyang on 2016/3/21.
 */
@Entity
@Table(name = "U_ACTIVITY")
public class Activity extends IdEntity
{
    public static final int TYPE_1 = 1; //单天
    public static final int TYPE_2 = 2; //连续多天
    public static final int TYPE_3 = 3; //每周期

    private String name;            //活动名称
    private String activityCode;    //活动CODE
    private String demo;            //活动简单
    private String imgs;            //图片
    private String startTime;       //开始日期
    private String endTime;         //结束日期
    private int type;               //活动开展类型
    private String address;         //活动地址
    private String tags;            //标签
    private String weekDay;         //周期
    private int needs;              //所需人数
    private int scope;              //签到误差范围
    private Volunteer monitor;      //活动负责人
    private Volunteer createUser;    //活动发布者
    private long times;              //累计产生的公益时间
    private int locked;             //是否已被封存
    private int delStatus;          //删除状态
    private int applys;             //报名数
    private int attends;            //参加人数
    private Group group;            //所属组织
    private String province;        //省市
    private String city;            //城市
    private String district;        //区县
    private Date createTime;        //发布日期
    private int goodComments;       //好评
    private int midComments;        //中评
    private int badComments;        //差评
    private int top;                //是否置顶
    private int selfGroup;          //是否只允许本社团人员增加
    private int status;             //活动进行状态
    private Double lng;             //活动地点经纬度
    private Double lat;
    private int browses;            //浏览量
    private String skill;           //本次活动所需求的服务技能
    private long oldID;
    private String platform;        //当前活动是属于哪些平台的

    public String getPlatform()
    {
        return platform;
    }

    public void setPlatform(String platform)
    {
        this.platform = platform;
    }

    public String getSkill()
    {
        return skill;
    }

    public void setSkill(String skill)
    {
        this.skill = skill;
    }

    public int getBrowses()
    {
        return browses;
    }

    public void setBrowses(int browses)
    {
        this.browses = browses;
    }

    @JsonIgnore
    public long getOldID()
    {
        return oldID;
    }

    public void setOldID(long oldID)
    {
        this.oldID = oldID;
    }

    public Double getLng()
    {
        return lng;
    }

    public void setLng(Double lng)
    {
        this.lng = lng;
    }

    public Double getLat()
    {
        return lat;
    }

    public void setLat(Double lat)
    {
        this.lat = lat;
    }

    public int getStatus()
    {
        return status;
    }

    public void setStatus(int status)
    {
        this.status = status;
    }

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "GROUP_ID")
    public Group getGroup()
    {
        return group;
    }

    public void setGroup(Group group)
    {
        this.group = group;
    }

    public Activity()
    {
    }

    public Activity(Long id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getActivityCode()
    {
        return activityCode;
    }

    public void setActivityCode(String activityCode)
    {
        this.activityCode = activityCode;
    }

    public String getDemo()
    {
        return demo;
    }

    public void setDemo(String demo)
    {
        this.demo = demo;
    }

    public String getImgs()
    {
        return imgs;
    }

    public void setImgs(String imgs)
    {
        this.imgs = imgs;
    }

    public String getStartTime()
    {
        return startTime;
    }

    public void setStartTime(String startTime)
    {
        this.startTime = startTime;
    }

    public String getEndTime()
    {
        return endTime;
    }

    public void setEndTime(String endTime)
    {
        this.endTime = endTime;
    }

    public int getType()
    {
        return type;
    }

    public void setType(int type)
    {
        this.type = type;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public String getTags()
    {
        return tags;
    }

    public void setTags(String tags)
    {
        this.tags = tags;
    }

    public String getWeekDay()
    {
        return weekDay;
    }

    public void setWeekDay(String weekDay)
    {
        this.weekDay = weekDay;
    }

    public int getNeeds()
    {
        return needs;
    }

    public void setNeeds(int needs)
    {
        this.needs = needs;
    }

    public int getScope()
    {
        return scope;
    }

    public void setScope(int scope)
    {
        this.scope = scope;
    }

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "MONITOR")
    public Volunteer getMonitor()
    {
        return monitor;
    }

    public void setMonitor(Volunteer monitor)
    {
        this.monitor = monitor;
    }

    // JPA 基于USER_ID列的多对一关系定义
    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "VOLUNTEER_ID")
    public Volunteer getCreateUser()
    {
        return createUser;
    }

    public void setCreateUser(Volunteer createUser)
    {
        this.createUser = createUser;
    }

    public long getTimes()
    {
        return times;
    }

    public void setTimes(long times)
    {
        this.times = times;
    }

    public int getLocked()
    {
        return locked;
    }

    public void setLocked(int locked)
    {
        this.locked = locked;
    }
    @JsonIgnore
    public int getDelStatus()
    {
        return delStatus;
    }

    public void setDelStatus(int delStatus)
    {
        this.delStatus = delStatus;
    }

    public int getApplys()
    {
        return applys;
    }

    public void setApplys(int applys)
    {
        this.applys = applys;
    }

    public int getAttends()
    {
        return attends;
    }

    public void setAttends(int attends)
    {
        this.attends = attends;
    }
    public String getProvince()
    {
        return province;
    }

    public void setProvince(String province)
    {
        this.province = province;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getDistrict()
    {
        return district;
    }

    public void setDistrict(String district)
    {
        this.district = district;
    }

    // 设定JSON序列化时的日期格式
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
    public Date getCreateTime()
    {
        return createTime;
    }

    public void setCreateTime(Date createTime)
    {
        this.createTime = createTime;
    }

    public int getGoodComments()
    {
        return goodComments;
    }

    public void setGoodComments(int goodComments)
    {
        this.goodComments = goodComments;
    }

    public int getMidComments()
    {
        return midComments;
    }

    public void setMidComments(int midComments)
    {
        this.midComments = midComments;
    }

    public int getBadComments()
    {
        return badComments;
    }

    public void setBadComments(int badComments)
    {
        this.badComments = badComments;
    }
    @JsonIgnore
    public int getTop()
    {
        return top;
    }

    public void setTop(int top)
    {
        this.top = top;
    }
    @JsonIgnore
    public int getSelfGroup()
    {
        return selfGroup;
    }

    public void setSelfGroup(int selfGroup)
    {
        this.selfGroup = selfGroup;
    }

//    @Override
//    public String toString() {
//        return ToStringBuilder.reflectionToString(this);
//    }
    public static String getQr(String activityCode)
    {
        String qr = "";
        if(StringUtils.isNotBlank(activityCode))
        {
            ByteArrayOutputStream out = QRCode.from(activityCode).to(ImageType.PNG).stream();
            byte[] data = out.toByteArray();
            qr = new String(Base64.encodeBase64(data));
            System.out.println(qr);
        }
        return qr;
    }
}
